package com.yusp75.myexam.ui.home

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView

import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.distinctUntilChanged
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import com.yusp75.myexam.R
import com.yusp75.myexam.databinding.FragmentHomeBinding
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.viewModel

class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null
    private val binding get() = _binding!!
    private val viewModel: HomeViewModel by viewModel()

    @SuppressLint("SetTextI18n")
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root

        // 选择题库
        binding.btnSelectBook.setOnClickListener {
            findNavController().navigate(R.id.nav_manage_books)
        }

        // 按钮：练习
        binding.btnExercise.setOnClickListener {
            val direction = HomeFragmentDirections.actionToDisplayFragment(btnPressed = "练习")
            findNavController().navigate(direction)
        }
        // 按钮：考试
        binding.btnExam.setOnClickListener {
            val direction = HomeFragmentDirections.actionToDisplayFragment(btnPressed = "考试")
            findNavController().navigate(R.id.nav_display)
        }

        // 绑定viewmodel
        binding.lifecycleOwner = viewLifecycleOwner
        binding.vm = viewModel  // 与xml中名一致
        return root
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // 更新选中的书
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                viewModel.currentBook.collect { bookName ->
                    binding.tvBookName.text = bookName
                }
            }
        }

        // 题目统计
        viewLifecycleOwner.lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                launch {
                    viewModel.totalCount.collect { count ->
                        binding.questionCount.text =
                            "总数: ${count.total}，选择：${count.choiceCount}，判断：${count.judgeCount}"
                    }
                }
                launch {
                    viewModel.statistic.collect { data ->
                        binding.statistic.text =
                            "总练习：${data.total}，答对：${data.totalCorrect}，答错：${data.totalWrong}"
                    }
                }
            }
        }

        //
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null

    }


}